package com.ewzj.bigdata.mvp.presenter;

import android.app.Application;

import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.CommentBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.ResCommentBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.ResIdBean;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.functions.Consumer;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.CommentListContract;

import java.util.List;


@ActivityScope
public class CommentListPresenter extends BasePresenter<CommentListContract.Model, CommentListContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private int page=1;
    @Inject
    public CommentListPresenter(CommentListContract.Model model, CommentListContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
    public void loadCommentListData(){
        page=1;
        mModel.getCommentList(mRootView.obtainArticleBean().getId()+"",page).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<CommentBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<CommentBean> commentBeans) {
                        page++;
                        mRootView.refresh(commentBeans);
                    }
                });
    }
    public void loadMoreCommentListData(){
        mModel.getCommentList(mRootView.obtainArticleBean().getId()+"",page).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new Consumer<List<CommentBean>>() {
                    @Override
                    public void accept(List<CommentBean> commentBeans) throws Exception {
                        mRootView.obtainCommentAdapter().loadMoreComplete();
                        page++;
                        mRootView.notifyDataAdd(commentBeans);
                        if (commentBeans.size()< AppConstants.PAGE_NUM){
                            mRootView.obtainCommentAdapter().loadMoreEnd();
                        }
                    }
                });
    }

    public void sendComment(String comment,String comid,String userid){
        mModel.sendComment(comment,comid,userid).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<ResCommentBean>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<ResCommentBean> resCommentBeanBaseData) {
                        mRootView.commentSendSuccess();
                    }
                });
    }
    public void sendReplyComment(String comment,String comid,String userid){
        mModel.replyComment(comment,comid,userid).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<ResCommentBean>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<ResCommentBean> resCommentBeanBaseData) {
                        mRootView.commentSendSuccess();
                    }
                });
    }
    public void commentPraise(String id){
        mModel.pariseComment(id).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<ResIdBean>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<ResIdBean> resIdBeanBaseData) {
                        mRootView.PraiseSuccess(resIdBeanBaseData.getData().getId());
                    }
                });
    }
}
